package com.liuwei.spring.cloud.junit.jdk;

import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;


@Slf4j
public class VirtualThread {

    public static void main(String[] args) throws InterruptedException {

        // 创建一个虚拟线程池
        ExecutorService virtualExecutor = Executors.newVirtualThreadPerTaskExecutor();
        // 提交100个任务到虚拟线程池
        for (int i = 0; i < 100; i++) {
            virtualExecutor.submit(() -> {
                // 模拟耗时操作
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.info("Task Virtual:{} Thread:{} completed",Thread.currentThread().isVirtual(),Thread.currentThread().threadId());
            });
        }

        // 关闭虚拟线程池
        virtualExecutor.shutdown();
        Thread.sleep(30000);
    }
}
